#coding=utf-8
import urllib2

user = 'k1007260765' 
password = '13567220098'
host_url = "http://www.xunsai.net:8000/?user=%s&password=%s&phonenumber=%s&text=%s&charset=gb2312"

test_sms ="你好，世界!"

class SendSmsFail_Error(Exception):
    
    def __init__(self, pn, text, content, info):
        self.content = content
        self.info = info
        self.pn = pn
        self.text = text
        
    def __str__(self):
        return "Send sms Fail(pn:%s)"%self.pn
            
def send_sms(pn, text):

    sended_text = unicode(text, 'u8').encode('gbk')
    url = host_url%(user, password, pn, sended_text)
    
    f = urllib2.urlopen(url)
    print f.getcode(), f.info(), f.read() 

    if f.code != 200:
        e = SendSmsFail_Error(pn, text, unicode(f.read(), 'gbk').encode('u8'), f.info())
        raise e

if __name__ == '__main__':
    send_sms('13567220098', test_sms)
